﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Windows.Forms;
using System.IO;
using System.Xml.Linq;

namespace JqlEditor
{
    internal class JqlConfigurationManager
    {
        public static readonly JqlConfigurationManager Manager = new JqlConfigurationManager();

        private XDocument ConfigDocument { get; set; }
        private string ConfigFilePath { get; set; }

        private string defaultOpenFileDirectory = String.Empty;
        private string defaultSaveAsFileDirectory = String.Empty;
        private ChessmanColor defaultChessmanColor = ChessmanColor.None;
        private string defaultFormSkinStyle = String.Empty;

        public string DefaultOpenFileDirectory
        {
            get
            {
                if (string.IsNullOrEmpty(defaultOpenFileDirectory))
                {
                    defaultOpenFileDirectory = ConfigDocument.Root.Element("defaultOpenFileDir").Value;
                }
                return defaultOpenFileDirectory;
            }
            set
            {
                if (defaultOpenFileDirectory != value)
                {
                    defaultOpenFileDirectory = value;
                    SaveConfigChange("defaultOpenFileDir", value);
                }
            }
        }
        public string DefaultSaveAsFileDirectory
        {
            get
            {
                if (string.IsNullOrEmpty(defaultSaveAsFileDirectory))
                {
                    defaultSaveAsFileDirectory = ConfigDocument.Root.Element("defaultSaveAsFileDir").Value;
                }
                return defaultSaveAsFileDirectory;
            }
            set
            {
                if (defaultSaveAsFileDirectory != value)
                {
                    defaultSaveAsFileDirectory = value;
                    SaveConfigChange("defaultSaveAsFileDir", value);
                }
            }
        }
        public ChessmanColor DefaultChessmanColor
        {
            get
            {
                if (defaultChessmanColor == ChessmanColor.None)
                {
                    var defaultColor = ConfigDocument.Root.Element("defaultChessmanColor").Value;
                    defaultChessmanColor = (ChessmanColor)Enum.Parse(typeof(ChessmanColor), defaultColor);
                }
                return defaultChessmanColor;
            }
            set
            {
                if (defaultChessmanColor != value)
                {
                    defaultChessmanColor = value;
                    SaveConfigChange("defaultChessmanColor", value);
                }
            }
        }
        public string DefaultFormSkinStyle
        {
            get
            {
                if (string.IsNullOrEmpty(defaultFormSkinStyle))
                {
                    defaultFormSkinStyle = ConfigDocument.Root.Element("defaultFormSkinStyle").Value;
                }
                return defaultFormSkinStyle;
            }
            set
            {
                if (defaultFormSkinStyle != value)
                {
                    defaultFormSkinStyle = value;
                    SaveConfigChange("defaultFormSkinStyle", value);
                }
            }
        }

        private void ReloadConfigFile()
        {
            ConfigDocument = XDocument.Load(ConfigFilePath);
        }
        private void SaveConfigChange(string elementName, object elementValue)
        {
            ConfigDocument.Root.Element(elementName).SetValue(elementValue);
            ConfigDocument.Save(ConfigFilePath);
            ReloadConfigFile();
        }

        private JqlConfigurationManager()
        {
            ConfigFilePath = Path.Combine(Application.StartupPath, "jqlConfigs.xml");
            ReloadConfigFile();
        }
    }
}
